package com.ruoyi.api.controller.lost;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.api.annotation.CurrentUser;
import com.ruoyi.api.annotation.IgnoreAuth;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.deal.bo.HyDealGoodsAddBo;
import com.ruoyi.deal.bo.HyDealGoodsQueryBo;
import com.ruoyi.deal.service.IHyDealGoodsService;
import com.ruoyi.deal.vo.HyDealGoodsVo;
import com.ruoyi.lost.bo.HyLostFoundQueryBo;
import com.ruoyi.lost.domain.HyLostFound;
import com.ruoyi.lost.service.IHyLostFoundService;
import com.ruoyi.system.domain.HyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 失物招领api接口
 * @author lixin
 * @date 2021-06-18 20:57
 */
@RestController
@RequestMapping("/api/lost")
public class ApiLostFoundController {

	@Autowired
	private IHyLostFoundService lostFoundService;

	/**
	 * 查询失物招领列表
	 */
	@IgnoreAuth
	@GetMapping("/list")
	public TableDataInfo<HyLostFound> list(HyLostFoundQueryBo bo) {
		if (ObjectUtil.isNull(bo.getStatus())) {
			bo.setStatus(0);
		}
		return lostFoundService.queryPageList(bo);
	}

	/**
	 * 查询我发布的
	 * @param user
	 * @param bo
	 * @return
	 */
	@GetMapping("/myList")
	public AjaxResult list(@CurrentUser HyUser user, HyLostFoundQueryBo bo) {
		bo.setUserId(user.getUserId());
		// 已解决
		if(ObjectUtil.isNotNull(bo.getResolved()) && bo.getResolved() == 1) {
			List<HyLostFound> list = lostFoundService.queryResolvedList(bo);
			return AjaxResult.success(list);
		}
		List<HyLostFound> list = lostFoundService.queryList(bo);
		return AjaxResult.success(list);
	}

	/**
	 * 查询我的认领
	 * @param user
	 * @return
	 */
	@GetMapping("/myClaim")
	public AjaxResult myClaim(@CurrentUser HyUser user) {
		List<HyLostFound> list = lostFoundService.queryListByClaimUserId(user.getUserId());
		return AjaxResult.success(list);
	}

	/**
	 * 查询我的归还
	 * @param user
	 * @return
	 */
	@GetMapping("/myReturn")
	public AjaxResult myReturn(@CurrentUser HyUser user) {
		List<HyLostFound> list = lostFoundService.queryListByReturnUserId(user.getUserId());
		return AjaxResult.success(list);
	}

	/**
	 * 获取失物招领详细信息
	 */
	@IgnoreAuth
	@GetMapping("/{foundId}")
	public AjaxResult<HyLostFound> getInfo(@PathVariable("foundId") Long foundId) {
		return AjaxResult.success(lostFoundService.queryById(foundId));
	}

	/**
	 * 获取失物招领发布用户
	 */
	@IgnoreAuth
	@GetMapping("/user/{foundId}")
	public AjaxResult<HyUser> getUserInfo(@PathVariable("foundId") Long foundId) {
		HyUser user = lostFoundService.queryUserByFoundId(foundId);
		return AjaxResult.success(user);
	}

	/**
	 * 失物招领发布
	 * @param user
	 * @param bo 失物招领信息
	 * @return
	 */
	@PostMapping("/publish")
	public AjaxResult publish(@CurrentUser HyUser user, @RequestBody HyLostFound bo) {
		if (StrUtil.isEmpty(user.getQqnumber())) {
			return AjaxResult.error("个人信息不完善，请在个人信息中填写联系QQ");
		}
		bo.setUserId(user.getUserId());

		Map<String, Object> resultObj = new HashMap<String, Object>(1);
		resultObj.put("foundId", lostFoundService.publish(bo));

		return AjaxResult.success(resultObj);
	}


	/**
	 * 删除帖子
	 * @param foundId
	 * @return
	 */
	@DeleteMapping("/delete/{foundId}")
	public AjaxResult delete(@PathVariable("foundId") Long foundId) {

		if(lostFoundService.deleteByFoundId(foundId)) {
			return AjaxResult.success("删除成功！");
		} else {
			return AjaxResult.error("删除失败！");
		}
	}

	/**
	 * 物品认领
	 * @param foundId
	 * @return
	 */
	@PutMapping("/claim/{foundId}")
	public AjaxResult claim(@CurrentUser HyUser user, @PathVariable("foundId") Long foundId) {

		if(lostFoundService.claim(user, foundId)) {
			return AjaxResult.success("认领成功！");
		} else {
			return AjaxResult.error("认领失败！");
		}
	}

	/**
	 * 物品归还
	 * @param foundId
	 * @return
	 */
	@PutMapping("/return/{foundId}")
	public AjaxResult returnFound(@CurrentUser HyUser user, @PathVariable("foundId") Long foundId) {

		if(lostFoundService.returnFound(user, foundId)) {
			return AjaxResult.success("归还成功！");
		} else {
			return AjaxResult.error("归还失败！");
		}
	}


}
